const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    show_info:{},
    car_list: [],
    car_type: [],
    navigator: { hide: 0 },
    tab_list: [
      { title: '首页', icon: 'fangzi' },
      { title: '车型报价', icon: 'chuzuche' },
      { title: '门店简介', icon: 'log' }
    ],
    tab_index: 0,
    is_display: false,
    sort_display: false,
    search_title: '全部品牌▼',
    sort_title: '默认排序▼',
    carBrandId: 0,
    sortNum: 0,
    src:[],
    is_myshop: false,
    fenxiangzhuan: {
      show: false,
      width: '86rpx;',
      height: '88rpx;',
      bottom: '380rpx;',
      top: '10rpx'
    }
  },
  //小程序码
  qrcode: function (e) {
    wx.showLoading({
      title: '拼命生成中',
    })
    var that = this
    wx.request({
      url: 'https://www.58zltc.com/api.php/Qrcode/getShopWxaCode/shop_id/' + that.data.info.shop_id + '/token/' + that.data.token,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var url = res.data.data.url;
        var urls = [];
        urls.push(url);
        wx.previewImage({
          urls: urls
        })
      },
      complete: function (e) {
        wx.hideLoading();
      }
    })
  },
  //快捷导航
  navigator: function (e) {
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1) {
      var offset = '0'
      var mum = 0;
    } else {
      var offset = width * 0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator: v
    })
  },

  backto: function (e) {
    wx.navigateBack({})
  },

  longtap: function (e) {
    var that = this;
    var url = e.currentTarget.dataset.src;
    this.data.src.push(url);
    wx.previewImage({
      urls: that.data.src
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var scene = decodeURIComponent(options.scene);
    if (scene != 'undefined') {
      var shop_id = scene;
    } else {
      var shop_id = options.shop_id;
    }
    this.setData({
      shop_id: shop_id
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    console.log(options)
    let that = this;
    wx.showLoading({
      title: '加载中…',
    })
    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator: v
        })
      },
    })

    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {

      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Shop/item/shop_id/' + shop_id + '/openid/' + app.globalData.openid, function (res) {
      if (res.data.openid == app.globalData.userInfo.openid) {
        that.setData({
          is_myshop: true
        })
      }
    })
    //ver-jionEndTime
    app.ihttpGet('https://www.58zltc.com/api.php/Car/verJionEndTime/shop_id/' + shop_id, function (res) {
      that.setData({
        verJoinEndTime: res.data
      })
    })
    //所有车型
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getAllCarBrand/shop_id/' +shop_id, function (res) {
      that.setData({
        car_type: res.data
      })
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getTypeCar/typeid/0/shop_id/' + shop_id, function(res){
      for (var index in res.data) {
        var str = res.data[index].title;
        var strArr = str.split(' ');
        res.data[index].titleAll = strArr[1] + '\n' + strArr[0] + '\t' + strArr[2] + '\t' + strArr[3];
        res.data[index].title = res.data[index].title.substring(0,15)+'...';
      }
      that.setData({
        car_list: res.data
      })
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Car/shop/shop_id/' + shop_id, function(res){
      var show_info;
      if (options.member_id) {
        res.data.member.forEach(function (value, index) {

          if (value.id == options.member_id) {
            show_info = { logo_url: value.headimg, name: value.name + '的网上车店', tel: value.tel }
          }
        })
      } else {
        show_info = { logo_url: res.data.logo_url, name: res.data.name, tel: res.data.tel }
      }
      that.setData({
        info: res.data,
        show_info: show_info
      })
      wx.setStorage({
        key: 'shop_show_info',
        data: show_info
      })
      wx.hideLoading();
    })
  },
  onReady: function () {
    app.confirmCommission()
  },
  setTab: function(e){
    this.setData({
      tab_index: e.currentTarget.dataset.index
    })
  },
  call: function () {
    const tel = this.data.show_info.tel;
    wx.makePhoneCall({
      phoneNumber: tel,
    })
  },
  openLocation: function(){
    const data = this.data.info;
    wx.openLocation({
      latitude: Number(data.latitude),
      longitude: Number(data.longitude),
      name: data.name,
      address: data.address
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    const shop = this.data.info;
    return {
      title: shop.name,
      path: '/pages/shop/car/detail?shop_id=' + shop.shop_id + '&fenxiang=' + app.globalData.openid + '&fWid=' + app.globalData.wid,
      success: function (res) {
        var a = that.data.fenxiangzhuan
        a.show = false
        that.setData({
          fenxiangzhuan: a
        })
        // 转发成功
        wx.showToast({
          title: '转发成功！',
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  clickMenuAll: function () {
    var that = this;
    if (this.data.is_display) {
      this.setData({
        search_title: that.data.search_title.replace('▲', '▼'), 
        is_display: false
      })
    } else {
      this.setData({
        search_title: that.data.search_title.replace('▼', '▲'), 
        is_display: true,
        sort_display: false
      })
    }
  },

  clickCarType: function (e) {
    const shop = this.data.shop_id;
    this.setData({
      carBrandId: e.currentTarget.dataset.id
    })
    var id = this.data.carBrandId;
    let that = this;
    //分类车型
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getTypeCar/typeid/' + id + '/shop_id/' + shop, function (res) {
      for (var index in res.data) {
        var str = res.data[index].title;
        var strArr = str.split(' ');
        res.data[index].titleAll = strArr[1] + '\n' + strArr[0] + '\t' + strArr[2] + '\t' + strArr[3];
        res.data[index].title = res.data[index].title.substring(0, 15) + '...';
      }
      that.setData({
        car_list: res.data
      })
    })
    this.setData({
      search_title: e.currentTarget.dataset.name + '▼',
      sort_title: '默认排序▼',
      is_display: false
    })
  },

  clickCarTypeBrand: function (e) {
    const shop = this.data.shop_id;
    this.setData({
      carBrandId: e.currentTarget.dataset.id
    })
    var id = this.data.carBrandId;
    let that = this;
    //分类车型
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getTypeCar/typeid/' + id +'/shop_id/' +shop, function (res) {
      for (var index in res.data) {
        var str = res.data[index].title;
        var strArr = str.split(' ');
        res.data[index].titleAll = strArr[1] + '\n' + strArr[0] + '\t' + strArr[2] + '\t' + strArr[3];
        res.data[index].title = res.data[index].title.substring(0, 15) + '...';
      }
      that.setData({
        car_list: res.data
      })
    })
    this.setData({
      tab_index: 1,
      search_title: e.currentTarget.dataset.name + '▼',
      sort_title: '默认排序▼',
      is_display: false
    })
  },

  clickMenuSort: function () {
    var that = this;
    if (this.data.sort_display) {
      this.setData({
        sort_title: that.data.sort_title.replace('▲', '▼'),
        sort_display: false
      })
    } else {
      this.setData({
        sort_title: that.data.sort_title.replace('▼', '▲'),
        sort_display: true,
        is_display: false
      })
    }
  },

  clickCarSort: function (e) {
    this.setData({
      sortNum: e.currentTarget.dataset.num
    })
    var sort_num = this.data.sortNum;
    let that = this;
    //分类车型
    app.ihttpGet('https://www.58zltc.com/api.php/Car/getSortCar/num/' + sort_num + '/typeid/' + that.data.carBrandId, function (res) {
      for (var index in res.data) {
        var str = res.data[index].title;
        var strArr = str.split(' ');
        res.data[index].titleAll = strArr[1] + '\n' + strArr[0] + '\t' + strArr[2] + '\t' + strArr[3];
        res.data[index].title = res.data[index].title.substring(0, 15) + '...';
      }
      that.setData({
        car_list: res.data
      })
    })
    this.setData({
      sort_title: e.currentTarget.dataset.name + '▼',
      sort_display: false
    })
  },
  copyTBL: function (e) {
    var that = this;
    var pages = getCurrentPages();
    var currentPage = pages[pages.length - 1];
    var url = currentPage.route;
    var options = currentPage.options;
    wx.setClipboardData({
      data: '/' + url + '?shop_id=' + options.shop_id,
      success: function (res) {
        // self.setData({copyTip:true}),  
        wx.showModal({
          title: '提示',
          content: '复制成功',
          success: function (res) {
            if (res.confirm) {
              console.log('确定')
            } else if (res.cancel) {
              console.log('取消')
            }
          }
        })
      }
    });
  },
  changeFXZ: function () {
    var that = this;
    var a = that.data.fenxiangzhuan
    a.show ? a.show = false : a.show = true
    that.setData({
      fenxiangzhuan: a
    })
  },
  jumpWeb: function () {
    var that =this
    if (that.data.info.web_link) {
      wx.navigateTo({
        url: '/pages/index/web/web?url=' + that.data.info.web_link,
      })
    }
  }
})